package com.ideal.frame.sys.controller;

import com.ideal.frame.common.anno.PermissionAnnotation;
import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.common.enumerate.ResponseCode;
import com.ideal.frame.common.enumerate.SecurityPermission;
import com.ideal.frame.sys.entity.SysUser;
import com.ideal.frame.sys.form.search.UserSearchForm;
import com.ideal.frame.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jin
 * @CreateTime: 2019-11-04 16:53
 * @Description: 用户管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理接口")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "分页查询用户列表", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("pages")
    @PermissionAnnotation(SecurityPermission.sys_user_view)
    public ResponseModel<Page<SysUser>> pages(@RequestBody UserSearchForm searchForm) {
        return ResponseModel.success(sysUserService.getPage(searchForm));
    }

    @ApiOperation(value = "根据id查询用户信息", httpMethod = "GET", response = ResponseModel.class)
    @GetMapping("getById/{id}")
    @PermissionAnnotation(SecurityPermission.sys_user_view)
    public ResponseModel<SysUser> getById(@PathVariable Long id) {
        return ResponseModel.success(sysUserService.getById(id));
    }

    @ApiOperation(value = "用户新增/修改", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("save")
    @PermissionAnnotation(SecurityPermission.sys_user_edit)
    public ResponseModel save(@RequestBody @Validated SysUser sysUser) {
        sysUserService.save(sysUser);
        return ResponseModel.success("保存成功");
    }

    @ApiOperation(value = "用户删除", httpMethod = "DELETE", produces = "application/json", response = ResponseModel.class)
    @DeleteMapping("del")
    @PermissionAnnotation(SecurityPermission.sys_user_del)
    public ResponseModel del(@RequestParam("ids") List<Long> ids) {
        sysUserService.userDel(ids);
        return ResponseModel.success("删除成功");
    }
}
